/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player;

import java.util.Iterator;
import java.util.LinkedList;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.actions.ActionMediator;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Stats;

/**
 * @author Massimo Canonaco
 *
 */
public class BattlePlayer extends SimplePlayer
{
	/** */
//	private SimplePlayer he;
	
	/** */
	private float skillPoints;
	
	/** */
	private int aliveCharacters;
	
	/** */
	private ActionMediator actions;

	/** */
	public BattlePlayer()
	{
//		this.he = he;
		aliveCharacters = 0;
		skillPoints = 0;
		characters = new LinkedList<>();
	}
	
	/** */
	public void update(float sp)
	{
		aliveCharacters = 0;
		skillPoints += sp;
		Stats temp = null;
		for(Character character : characters)
		{	
			if(character.isAlive())
			{
				aliveCharacters++;
				
				// If character is moved, restore his initial range.
				if(character.isMoved())
				{
					// Get character's statics
					temp = character.getStats().getCharacteristics();

					// Reset his range
					temp.setMovementRange(temp.getMovementRange()+character.getTraveled());

					// Update the travel distance to zero.
					character.setTraveled(0);

					// Now it's not moved anymore
					character.makeUnmoved();
				}
			}
		}
		//TODO: resetting the ability and some statics?!
	}
	
	/** */
	public void subtractSkillPoints(final float value)
	{
		skillPoints -= value;
	}
	
	/** */
	public boolean hasAliveCharacters()
	{
		return aliveCharacters > 0;
	}

	/**
	 * @return the skillPoints
	 */
	public float getSkillPoints() {
		return skillPoints;
	}

	/**
	 * @return the actions
	 */
	public ActionMediator getActions() {
		return actions;
	}
	
	/** */
	public void setActions(ActionMediator actions)
	{
		this.actions = actions;
	}
	
	/** */
	public void addCharacter(final Character character)
	{
		character.setAlive(true);
		characters.add(character);
		
		aliveCharacters++;
	}
	
	/** */
	synchronized public void removeCharacter(final Character character)
	{
		for (Character subj : characters) 
		{
			if(character == subj)
			{
				characters.remove(subj);
				character.setAlive(false);
				aliveCharacters--;
			}
			
		}
	}

	public void disband() 
	{
//		Iterator<Character> iteratorList = characters.iterator();
//		while(iteratorList.hasNext())
//			iteratorList.remove();
		characters = new LinkedList<>();
//		characters.removeAll(characters);
		aliveCharacters = 0;
		skillPoints = 0;
		
	}
}
